package com.tarcemap.gpsmobileappgui;



import com.tracemap.gpslocation.GPSManager;
import com.tracemap.gpslocation.GPSReadListener;
import com.tracemap.gpslocation.R;
import com.tracemap.gpslocation.R.id;
import com.tracemap.services.LocationSender;
import com.tracemap.services.ServiceCaller;
import com.tracemap.services.ServiceCaller.Binding;
import com.tracemap.websender.ReportLocation;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WannaDebugTableActivity extends ActivityGroup  implements OnClickListener,
		GPSReadListener {
	//private WannaMainActivity wannaMain;
	

	

	TextView t1;
	TextView t2;

	Button butGps;
	Button butUpload;
	Button butRefresh;

	private boolean mToggleIndeterminate = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//LinearLayout frameLayout = (LinearLayout) findViewById(R.id.tab_debug_id);
		setContentView(R.layout.debugview);

        
		t1 = (TextView) findViewById(R.id.latText);
		t2 = (TextView) findViewById(R.id.latText1);
		butGps = (Button) findViewById(R.id.butGps);
		butRefresh = (Button) findViewById(R.id.butRefresh);
		butUpload = (Button) findViewById(R.id.butUpload);
		
		butGps.setOnClickListener(this);
		butRefresh.setOnClickListener(this);
		butUpload.setOnClickListener(this);
		
		t1.setText("0.0");
		t2.setText("0.0");
		
        
	}

	
	//ServiceCaller.Binding binding;
	public void onClick(View v) {
		//binding = new ServiceCaller.Binding(this);
		if (v == butGps) {
			mToggleIndeterminate = !mToggleIndeterminate;
			if (mToggleIndeterminate)				
			{
				Log.i("onClick","Bind service");
				butGps.setText("UnBind service");
				this.doBindService();
			}else
			{
				butGps.setText("Bind service");
				this.doUnbindService();
			}
			
			//setProgressBarIndeterminateVisibility(mToggleIndeterminate);
//			if (gps == null) {
//				
//
//				butGps.setText("Stop Trace");
//			} else {
//				
//				t1.setText("0.0");
//				t2.setText("0.0");
//				butGps.setText("Start Trace");
//			}
			

		} else if (v == butRefresh) {
			
			
		}else if (v == butUpload) {
			ReportLocation rl= new ReportLocation();
			rl.post(String.valueOf(System.currentTimeMillis()), t1.getText().toString(), t2.getText().toString());
		}

	}

	@Override
	public void read(double lat, double lng, double alt) {
		Message msg = Message.obtain();
		msg.what=10;
		msg.obj=new String[]{String.valueOf(lat),String.valueOf(lng)};
		
		updataHandler.sendMessage(msg);

	}

	@Override
	public void readException(Exception ex) {
		// TODO Auto-generated method stub

	}

	@Override
	public void readError(Exception ex, String errorMessage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void TimeOut() {
		// TODO Auto-generated method stub

	}

	@Override
	public void choseDevices() {
		// TODO Auto-generated method stub

	}

	Handler updataHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int state = msg.what;
			if (state == 10) {
				String opts[] = (String[]) msg.obj;
				t1.setText(opts[0]);
				t2.setText(opts[1]);
			}
		}
	};

	private boolean mIsBound;

    private GPSReadListener mListener;
    private LocationSender mBoundService;
    
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mBoundService = ((LocationSender.LocalBinder)service).getService();
            mBoundService.setmGPSListener(mListener);
            // Tell the user about this for our demo.
            Toast.makeText(WannaDebugTableActivity.this, R.string.local_service_connected,
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            mBoundService = null;                
            Toast.makeText(WannaDebugTableActivity.this, R.string.local_service_disconnected,
                    Toast.LENGTH_SHORT).show();
        }
    };
    
    public void OnCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    }
    
    //public Binding(GPSReadListener listener)
    //{
    //	mListener = listener;
    //}
    
    public void doBindService() {
        // Establish a connection with the service.  We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).
    	Intent me = new Intent(this, LocationSender.class);
    	this.getApplicationContext().bindService(me, mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }
    
    public void doUnbindService() {
        if (mIsBound) {
            // Detach our existing connection.
        	this.getApplicationContext().unbindService(mConnection);
            mIsBound = false;
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }


}